package com.huang825172.sbog.Tabs.Mine.LoginPage.View;

import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huang825172.sbog.BaseFlux.Dispatcher;
import com.huang825172.sbog.BaseFlux.Store.IDataChangedListener;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Mine.LoginPage.Action.ActionCreator;
import com.huang825172.sbog.Tabs.Mine.LoginPage.Store.Store;
import com.huang825172.sbog.Tabs.Mine.RegisterPage.View.RegisterPage;
import com.huang825172.sbog.Util.TAGs;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginPage extends AppCompatActivity implements View.OnClickListener, IDataChangedListener {

    @BindView(R.id.LoginPageConfirmButton)
    Button confirmButton;
    @BindView(R.id.LoginPageIDEditText)
    EditText idEditText;
    @BindView(R.id.LoginPagePWEditText)
    EditText pwEditText;

    @BindView(R.id.LoginPagePWRememberTextView)
    TextView pwRememberTextView;
    @BindView(R.id.LoginPagePWUtilCheckBoxImageView)
    ImageView pwRememberImageView;

    @BindView(R.id.LoginPageToRegisterTextView)
    TextView toRegisterTextView;

    @BindView(R.id.LoginPageRootView)
    ConstraintLayout rootView;

    //Flux结构组件
    private Store store;
    private ActionCreator creator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);

        ButterKnife.bind(this);
        init();
    }

    private void init() {
        //Flux结构绑定
        store = new Store(getApplicationContext());
        creator = new ActionCreator();
        Dispatcher.getSingleton().register(store);
        store.setDataChangedListener(this);

        //监听绑定
        confirmButton.setOnClickListener(this);
        pwRememberTextView.setOnClickListener(this);
        pwRememberImageView.setOnClickListener(this);
        toRegisterTextView.setOnClickListener(this);

        //保存的用户名密码
        if (store.getLoginRememberStatus()) {
            pwEditText.setText(store.getRememberPassword());
            idEditText.setText(store.getRememberUsername());
            pwRememberImageView.setImageDrawable(getDrawable(R.mipmap.login_checkbox_clicked));
        } else {
            pwRememberImageView.setImageDrawable(getDrawable(R.mipmap.login_checkbox_normal));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.LoginPageConfirmButton: {
                String username = idEditText.getText().toString();
                String password = pwEditText.getText().toString();
                creator.createLoginAction(username, password);
                break;
            }
            case R.id.LoginPagePWRememberTextView: {
                String username = idEditText.getText().toString();
                String password = pwEditText.getText().toString();
                creator.createRememberAction(username, password);
                break;
            }
            case R.id.LoginPagePWUtilCheckBoxImageView: {
                String username = idEditText.getText().toString();
                String password = pwEditText.getText().toString();
                creator.createRememberAction(username, password);
                break;
            }
            case R.id.LoginPageToRegisterTextView: {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), RegisterPage.class);
                startActivity(intent);
                finish();
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onDataChanged(String label) {
        switch (label) {
            case "LoginPageLoginAction": {
                if (store.getLoginStatus()) {
                    new com.huang825172.sbog.Tabs.Mine.MinePage.Action.ActionCreator().createRefleshAction();
                    finish();
                } else {
                    Snackbar.make(rootView, "LoginFail", Snackbar.LENGTH_SHORT).show();
                    Log.e(TAGs.ERROR, store.getExceptionString());
                }
                break;
            }
            case "LoginPageRememberAction": {
                if (store.getLoginRememberStatus()) {
                    pwRememberImageView.setImageDrawable(getDrawable(R.mipmap.login_checkbox_clicked));
                } else {
                    pwRememberImageView.setImageDrawable(getDrawable(R.mipmap.login_checkbox_normal));
                }
                break;
            }
            default:
                break;
        }
    }
}
